# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

_realname=html5lib
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}" "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=1.0.1
pkgrel=1
arch=('any')
pkgdesc="A Python HTML parser/tokenizer based on the WHATWG HTML5 spec (mingw-w64)"
url="https://github.com/html5lib"
license=('MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-python3-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python2-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python3-webencodings"
             "${MINGW_PACKAGE_PREFIX}-python2-webencodings")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python3-six" "${MINGW_PACKAGE_PREFIX}-python2-six"
              "${MINGW_PACKAGE_PREFIX}-python2-pytest" "${MINGW_PACKAGE_PREFIX}-python3-pytest" 
              "${MINGW_PACKAGE_PREFIX}-python3-lxml" "${MINGW_PACKAGE_PREFIX}-python2-lxml"
              "${MINGW_PACKAGE_PREFIX}-python3-mock" "${MINGW_PACKAGE_PREFIX}-python2-mock") 
source=("${_realname}-${pkgver}.tar.gz::https://github.com/html5lib/html5lib-python/archive/${pkgver}.tar.gz"
        LICENSE)
sha256sums=('fabbebd6a55d07842087f13849076eeed350aa8bb6c9ec840f6a6aba9388db06'
            '89807acf2309bd285f033404ee78581602f3cd9b819a16ac2f0e5f60ff4a473e')

prepare() {  
  cd "${srcdir}"
  for pver in {2,3}; do 
    rm -rf python${pver}-build-${CARCH} | true
    cp -r "${_realname}-python-${pkgver}" "python${pver}-build-${CARCH}"
  done
}

# Note that build() is sometimes skipped because it's done in 
# the packages setup.py install for simplicity if you can do so.
# but sometimes, you want to do a check before install which would
# also trigger the build.  
build() {  
  for pver in {2,3}; do  
    msg2 "Python ${pver} build for ${CARCH}"  
    cd "${srcdir}/python${pver}-build-${CARCH}"
    ${MINGW_PREFIX}/bin/python${pver} setup.py build
  done
}

check() {
#A few tests fail but most succede.
    for pver in {2,3}; do
      msg2 "Python ${pver} check for ${CARCH}"
      cd ${srcdir}/python${pver}-build-${CARCH}/html5lib/tests
      ${MINGW_PREFIX}/bin/nosetests${pver} || true
    done
}

package_python3-html5lib() {
  depends=("${MINGW_PACKAGE_PREFIX}-python3" 
           "${MINGW_PACKAGE_PREFIX}-python3-six" 
           "${MINGW_PACKAGE_PREFIX}-python3-webencodings")
    cd "${srcdir}/python3-build-${CARCH}"

    MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build
    install -Dm755 $srcdir/LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/python2-${_realname}/LICENSE
}

package_python2-html5lib() {
    depends=("${MINGW_PACKAGE_PREFIX}-python2" 
             "${MINGW_PACKAGE_PREFIX}-python2-six"
             "${MINGW_PACKAGE_PREFIX}-python2-webencodings")
    cd "${srcdir}/python2-build-${CARCH}"

    MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build
    install -Dm755 $srcdir/LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/python3-${_realname}/LICENSE
}

package_mingw-w64-i686-python2-html5lib() {
  package_python2-html5lib
}

package_mingw-w64-i686-python3-html5lib() {
  package_python3-html5lib
}

package_mingw-w64-x86_64-python2-html5lib() {
  package_python2-html5lib
}

package_mingw-w64-x86_64-python3-html5lib() {
  package_python3-html5lib
}
